﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="FileUtility.cs" company="">
//   
// </copyright>
// <summary>
//   The file utility.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.Utilities
{
    using System;
    using System.IO;
    using System.Reflection;

    /// <summary>
    ///   The file utility.
    /// </summary>
    public static class FileUtility
    {
        #region Public Methods and Operators

        /// <summary>
        /// The copy to temporary file.
        /// </summary>
        /// <param name="filePath">
        /// The file path. 
        /// </param>
        /// <param name="keepName">
        /// The keep name. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        public static string CopyToTemporaryFile(string filePath, bool keepName = true)
        {
            var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(filePath);
            var extension = Path.GetExtension(filePath);
            if (keepName)
            {
                fileNameWithoutExtension = fileNameWithoutExtension + "." + Path.GetRandomFileName();
            }
            else
            {
                fileNameWithoutExtension = Path.GetRandomFileName();
            }

            var temporaryFilePath = Path.Combine(Path.GetTempPath(), fileNameWithoutExtension + extension);
            File.Copy(filePath, temporaryFilePath, true);
            return temporaryFilePath;
        }

        /// <summary>
        /// The get file size string.
        /// </summary>
        /// <param name="filePath">
        /// The file path. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        public static string GetFileSizeString(string filePath, int digitsAfterZero = 2)
        {
            var file = new FileInfo(filePath);
            var size = file.Length;
            if (size < Sizes.Kilobyte)
            {
                return size + " B";
            }
            else if (size < Sizes.Megabyte)
            {
                return Math.Round(((double)size) / Sizes.Kilobyte, digitsAfterZero) + " KB";
            }
            else if (size < Sizes.Gigabyte)
            {
                return Math.Round(((double)size) / Sizes.Megabyte, digitsAfterZero) + " MB";
            }
            else if (size < Sizes.Terrabyte)
            {
                return Math.Round(((double)size) / Sizes.Gigabyte, digitsAfterZero) + " GB";
            }

            return "Too big :)";
        }

        /// <summary>
        /// The get temporary file path.
        /// </summary>
        /// <param name="fileName">
        /// The file name. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        public static string GetTemporaryFilePath(string fileName)
        {
            var randomDirectoryName = Path.GetFileNameWithoutExtension(Path.GetRandomFileName());
            var directoryPath = Path.Combine(
                Path.GetTempPath(), Assembly.GetEntryAssembly().GetName().Name, randomDirectoryName);
            Directory.CreateDirectory(directoryPath);
            return Path.Combine(directoryPath, fileName);
        }

        /// <summary>
        ///   The get temporary file path without extension.
        /// </summary>
        /// <returns> The <see cref="string" /> . </returns>
        public static string GetTemporaryFilePathWithoutExtension()
        {
            var directoryPath = Path.Combine(Path.GetTempPath(), Assembly.GetEntryAssembly().GetName().Name);
            Directory.CreateDirectory(directoryPath);
            return Path.Combine(directoryPath, Path.GetRandomFileName());
        }

        #endregion

        // TODO Remove???
        // public static Stream ReadFromUrl(string url)
        // {
        // 	var request = WebRequest.Create(url);
        // 	var response = request.GetResponse();
        // 	var mimeType = MimeTypes.GetByContentType(response.ContentType);
        // 	return response.GetResponseStream();
        // }

        // public static Task<IFile> GetFromUrlAsync(string url)
        // {
        // 	var request = WebRequest.Create(url);
        // 	//TODO Upgrade to await and GetResponseAsync in 4.5
        // 	var responseTask = Task<WebResponse>.Factory.FromAsync(request.BeginGetResponse, request.EndGetResponse, null);
        // 	var continueTask = responseTask.ContinueWith((t) =>
        // 		{
        // 			var mimeType = MimeTypes.GetByContentType(t.Result.ContentType);
        // 			return (IFile) new MemoryFile(null, mimeType.FileExtension, t.Result.GetResponseStream());
        // 		});
        // 	return continueTask;
        // }
    }
}